package com.cuit.qx.service;

import com.cuit.qx.bean.PageList;
import com.cuit.qx.bean.User;
import com.cuit.qx.dao.UserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserInfoService {
	
	@Autowired
	private UserMapper userMapper;
	
//	public PageList<User> getAllUser(){
//		logger.info("===================进入业务层=====================");
//		PageHelper.startPage(1, 10); // 核心分页代码
//		List<User> list = userMapper.selectAllUser();
//		return new PageList<User>(list);
//	}

	/**
	 * 登录
	 * @param userName
	 * @param password
	 * @return
	 */
	public User login(String userName, String password) {
//		return userMapper.selectUserLogin(userName,StringUtils.SHA256(new String(RSAUtils.decode(password))));
		return userMapper.selectUserLogin(userName,password);
	}

	public User getUser(String userName) {
//		return userMapper.selectUserLogin(userName,StringUtils.SHA256(new String(RSAUtils.decode(password))));
		return userMapper.selectUser(userName);
	}

	
	/**
	 * 查询错误登录次数
	 * @param userName
	 * @return
	 */
	public Long getUserErrorCount(String userName) {
		return userMapper.selectUserErrorCount(userName);
	}

	/**
	 * 查询用户锁定时间
	 * @param userName
	 * @return
	 */
	public Date getLockDate(String userName) {
		return userMapper.selectUserLockDate(userName);
	}

	/**
	 * 更新账户锁定时间
	 * @param userName
	 * @param date
	 */
	public void updateUserCountAndLockDate(String userName, Date date) {
		userMapper.updateUserCountAndLockDate(userName,date);
	}

	/**
	 * 更新账户锁定次数
	 * @param userName
	 * @param i
	 */
	public void updateUserErrorCount(String userName, int i) {
		userMapper.updateUserErrorCount(userName,i);
	}

	/**
	 * 根据id删除用户
	 * @param id
	 * @return
	 */
	public int deleteUser(String id) {
		 return userMapper.deleteUserById(id);
	}

	
	/**
	 * 查询用户列表
	 * @param start
	 * @param pageSize
	 * @param linkName
	 * @param type
	 * @return
	 */
	public List<User> queryList(int start, int pageSize, String linkName,
			Integer type) {
		List<User> list = userMapper.selectAllUser(start,pageSize,linkName,type);
		return list;
	}

	/**
	 * 统计用户列表
	 * @param linkName
	 * @param type
	 * @return
	 */
	public int queryListCount(String linkName, Integer type) {
		int count = userMapper.selectCountUser(linkName,type);
		return count;
	}

	/**
	 * 批量删除用户
	 * @param ids
	 * @return
	 */
	public int deleteUsers(String ids) {
		return userMapper.deleteUsersByIds(ids.split(","));
	}

	/**
	 * 新增用户
	 * @param user
	 */
	public void addUser(User user) {
		userMapper.insertUser(user);
	}

	/**
	 * 查询登录名是否重复
	 * @param loginname
	 * @return
	 */
	public int checkLoginName(String loginname) {
		return userMapper.checkLoginName(loginname);
	}

	public User getUserInfoBeanById(String id) {
		return userMapper.selectUserInfoBeanById(id);
	}

	/**
	 * 修改用户
	 * @param user
	 * @return
	 */
	public int updateUserInfoBean(User user) {
		return userMapper.updateUserInfoBean(user);
	}

	/**
	 * 改变用户状态
	 * @param
	 * @param status
	 * @return
	 */
	public int changeUserStatus(String userid, String status) {
		return userMapper.updateUserStatus(userid,Integer.parseInt(status));
	}

	/**
	 * 查询密码是否正确
	 * @param account
	 * @param password
	 * @return
	 */
	public int isPassword(String account, String password) {
		return userMapper.selectPasswordCon(account,password);
	}

	/**
	 * 修改密码
	 * @param account
	 * @param password
	 * @return
	 */
	public int updatePassword(String account, String password) {
		return userMapper.updatePassword(account,password);
	}

}
